package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.base.BaseDao;
import com.atguigu.base.BaseServiceImpl;
import com.atguigu.dao.CommunityDao;
import com.atguigu.dao.DictDao;
import com.atguigu.entity.Community;
import com.atguigu.entity.House;
import com.atguigu.service.CommunityService;
import com.atguigu.util.CastUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CommunityServiceImpl
 * @Description 此类描述:
 * @Author dangchen
 * @DateTime 2022-08-24 10:15
 * @Version 1.0
 */
@Service(interfaceClass = CommunityService.class)
@Transactional
public class CommunityServiceImpl extends BaseServiceImpl<Community> implements CommunityService {

    @Autowired
    private CommunityDao communityDao;

    @Autowired
    private DictDao dictDao;

    @Override
    protected BaseDao<Community> getEntityDao() {
        return communityDao;
    }

    @Override
    public PageInfo<Community> findPage(Map<String, Object> filters) {
        //当前页
        int pageNum = CastUtil.castInt(filters.get("pageNum"), 1);
        //每页显示的记录条数
        int pageSize = CastUtil.castInt(filters.get("pageSize"), 5);

        PageHelper.startPage(pageNum,pageSize);
        Page<Community> page =  getEntityDao().findPage(filters);
        //给每一个Community指定areaName和plateName
        //单表查询用
       /*List<Community> communityList = page.getResult();
        for (Community community : communityList){
            String areaName = dictDao.getNameById(community.getAreaId());
            community.setAreaName(areaName);
            String plateName = dictDao.getNameById(community.getPlateId());
            community.setPlateName(plateName);
        }*/
        return new PageInfo<>(page,3);
    }

    @Override
    public List<Community> findAll() {
        return communityDao.findAll();
    }

    @Override
    public Community getById(Serializable id) {
        Community community = communityDao.getById(id);
        String areaName = dictDao.getNameById(community.getAreaId());
        community.setAreaName(areaName);
        String plateName = dictDao.getNameById(community.getPlateId());
        community.setPlateName(plateName);
        return community;
    }
}
